package com.seaboxdata.interfaceJob;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.seaboxdata.dao.YtcDao;
import com.seaboxdata.entity.CreditCard;
import com.seaboxdata.kafka.BaseKafkaConsumer;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.sm4.MD5;
import com.seaboxdata.utils.HttpUtil;
import com.seaboxdata.utils.HttpUtils;
import com.seaboxdata.utils.RabbitContUtil;
import org.apache.avro.util.Utf8;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.hibernate.loader.collection.OneToManyJoinWalker;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class YtcJob {
    @Resource
    private YtcDao ytcDao;

    @Autowired
    private BaseKafkaConsumer baseKafkaConsumer;
    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;
    public Map<String,String> signature(){
        Map ruselt=new HashMap();
        long l = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        System.out.println(l);
        String signature = MD5.MD5Encode("dpxqzwfwsjglj"+"WDd@$^632323"+l+"b3f071832a231232a5cea23243f1db04", "utf8");
        System.out.println(signature);
        ruselt.put("signature", signature);
        ruselt.put("time", l);
        return ruselt;
    }

    public static void main(String[] args) {
        YtcJob ytcJob=new YtcJob();
         ytcJob.getAreaList();
        //System.out.println(signature);
    }
    public void getCantonList(){
        LocalDateTime dateTime=LocalDateTime.now();
        String url="http://192.168.34.96:62014/jw-api/api/rtc/getCantonList";
        url=url+"?opCode=dpxqzwfwsjglj&signature="+signature().get("signature")+"&timeStamp="+signature().get("time");
        HttpUtils.Response response = HttpUtils.doGet(url, "UTF-8", null);
        String responseData = response.getResponseData();
        JSONObject jsonObject= JSONObject.parseObject(responseData);
        Object datasObject = jsonObject.get("data");
        List<Map<String , Object>> datas=new ArrayList<>();
        if(null!=datasObject){
            JSONArray jsonArray = JSONArray.parseArray(datasObject.toString());
            for (Object data : jsonArray) {
                Map map=new HashMap();
                JSONObject dataObject=JSONObject.parseObject(data.toString());
                Object cantonId = dataObject.get("CantonId");
                Object cantonName = dataObject.get("CantonName");
                map.put("CantonId", cantonId);
                map.put("CantonName", cantonName);
                map.put("cjsj",LocalDateTime.now());
                datas.add(map);
            }
            //清空表，插入数据
            ytcDao.delCantonList();
            ytcDao.insertCantonList(datas);
        }
    }

    public void getAreaList(){
        String cantonId="9";//ytcDao.selectCantonByName("大鹏新区");
        String url="http://192.168.34.96:62014/jw-api/api/rtc/getAreaList";
        url=url+"?opCode=dpxqzwfwsjglj&signature="+signature().get("signature")+"&timeStamp="+signature().get("time")+"&cantonId="+cantonId;
        HttpUtils.Response response = HttpUtils.doGet(url, "UTF-8", null);
        String responseData = response.getResponseData();
        JSONObject jsonObject= JSONObject.parseObject(responseData);
        Object datasObject = jsonObject.get("data");
        List<Map<String , Object>> datas=new ArrayList<>();
        if(null!=datasObject){
            JSONArray jsonArray = JSONArray.parseArray(datasObject.toString());
            for (Object data : jsonArray) {
                Map map=new HashMap();
                JSONObject dataObject=JSONObject.parseObject(data.toString());
                Object areaName = dataObject.get("AreaName");
                Object areaId = dataObject.get("AreaId");
                map.put("AreaName", areaName);
                map.put("AreaId", areaId);
                map.put("cantonId",cantonId);
                map.put("cjsj",LocalDateTime.now());
                datas.add(map);
            }
            //清空表，插入数据
            ytcDao.delAreaList();
            ytcDao.insertAreaList(datas);
        }
    }

    public void getSectionList(){
        String cantonId="9";//ytcDao.selectCantonByName("大鹏新区");
        String url="http://192.168.34.96:62014/jw-api/api/rtc/getSectionList";
        url=url+"/opCode=/signature="+signature().get("signature")+"/timeStamp="+signature().get("time")+"cantonId="+cantonId;;
        HttpUtils.Response response = HttpUtils.doGet(url, "UTF-8", null);
        String responseData = response.getResponseData();
        JSONObject jsonObject= JSONObject.parseObject(responseData);
        Object datasObject = jsonObject.get("data");
        List<Map<String , Object>> datas=new ArrayList<>();
        if(null!=datasObject){
            JSONArray jsonArray = JSONArray.parseArray(datasObject.toString());
            for (Object data : jsonArray) {
                Map map=new HashMap();
                JSONObject dataObject=JSONObject.parseObject(data.toString());
                Object sectionLatitude = dataObject.get("SectionLatitude");
                Object sectionName = dataObject.get("SectionName");
                Object sectionLongitude = dataObject.get("SectionLongitude");
                Object sectionId = dataObject.get("SectionId");
                map.put("SectionLatitude", sectionLatitude);
                map.put("SectionName", sectionName);
                map.put("SectionLongitude", sectionLongitude);
                map.put("SectionId", sectionId);
                map.put("cjsj",LocalDateTime.now());
                datas.add(map);
            }
            //清空表，插入数据
            ytcDao.delSectionList();
            ytcDao.insertSectionList(datas);
        }
    }

    public void getBerthList(){
        LocalDateTime dateTime=LocalDateTime.now();
        String cantonId=ytcDao.selectCantonByName("大鹏新区");
        String url="http://192.168.34.96:62014/jw-api/api/rtc/getBerthList";
        url=url+"?opCode=/signature="+signature().get("signature")+"&timeStamp="+signature().get("time")+"cantonId="+cantonId+"&pageIndex=1"+"&pageSize=1000"+"&berthCode=";
        HttpUtils.Response response = HttpUtils.doGet(url, "UTF-8", null);
        String responseData = response.getResponseData();
        JSONObject jsonObject= JSONObject.parseObject(responseData);
        Object datasObject = jsonObject.get("data");
        List<Map<String , Object>> datas=new ArrayList<>();
        if(null!=datasObject){
            JSONArray jsonArray = JSONArray.parseArray(datasObject.toString());
            for (Object data : jsonArray) {
                Map map=new HashMap();
                JSONObject dataObject=JSONObject.parseObject(data.toString());
                Object AreaName = dataObject.get("AreaName");
                Object ManufacturerName = dataObject.get("ManufacturerName");
                Object PricingStrategyName = dataObject.get("PricingStrategyName");
                Object BerthAndBerthType = dataObject.get("BerthAndBerthType");
                Object SetPosition = dataObject.get("SetPosition");
                Object LineDirection = dataObject.get("LineDirection");
                Object ParkStatus = dataObject.get("ParkStatus");
                Object BerthStatus = dataObject.get("BerthStatus");
                Object CantonName = dataObject.get("CantonName");
                Object SectionName = dataObject.get("SectionName");
                Object BerthCode = dataObject.get("BerthCode");
                map.put("AreaName", AreaName);
                map.put("ManufacturerName", ManufacturerName);
                map.put("PricingStrategyName", PricingStrategyName);
                map.put("BerthAndBerthType", BerthAndBerthType);
                map.put("SetPosition", SetPosition);
                map.put("LineDirection", LineDirection);
                map.put("ParkStatus", ParkStatus);
                map.put("BerthStatus", BerthStatus);
                map.put("CantonName", CantonName);
                map.put("SectionName", SectionName);
                map.put("BerthCode", BerthCode);
                map.put("cjsj",LocalDateTime.now());
                datas.add(map);
            }
            //清空表，插入数据
            ytcDao.delBerthList();
            ytcDao.insertBerthList(datas);
        }
    }

    public void getRTCPrice(){
        LocalDateTime dateTime=LocalDateTime.now();
        String cantonId=ytcDao.selectCantonByName("大鹏新区");
        String url="http://192.168.34.96:62014/jw-api/api/rtc/getRTCPrice";
        url=url+"?opCode=/signature="+signature().get("signature")+"&timeStamp="+signature().get("time")+"cantonId="+cantonId;;
        HttpUtils.Response response = HttpUtils.doGet(url, "UTF-8", null);
        String responseData = response.getResponseData();
        JSONObject jsonObject= JSONObject.parseObject(responseData);
        Object datasObject = jsonObject.get("data");
        List<Map<String , Object>> datas=new ArrayList<>();
        if(null!=datasObject){
            JSONArray jsonArray = JSONArray.parseArray(datasObject.toString());
            for (Object data : jsonArray) {
                Map map=new HashMap();
                JSONObject dataObject=JSONObject.parseObject(data.toString());
                Object state = dataObject.get("state");
                Object msg = dataObject.get("msg");
                Object PricingStrategyName = dataObject.get("PricingStrategyName");
                Object WorkPricing = dataObject.get("WorkPricing");
                Object NoWorkPricing = dataObject.get("NoWorkPricing");
                Object IsAllow = dataObject.get("IsAllow");
                Object ParkTime = dataObject.get("ParkTime");
                Object DateType = dataObject.get("DateType");
                map.put("state", state);
                map.put("msg", msg);
                map.put("PricingStrategyName", PricingStrategyName);
                map.put("NoWorkPricing", NoWorkPricing);
                map.put("WorkPricing", WorkPricing);
                map.put("IsAllow", IsAllow);
                map.put("ParkTime", ParkTime);
                map.put("DateType", DateType);
                map.put("cjsj",LocalDateTime.now());
                datas.add(map);
            }
            //清空表，插入数据
            ytcDao.delRTCPrice();
            ytcDao.insertRTCPrice(datas);
        }
    }

    public void getNearRTCInfo(){
        LocalDateTime dateTime=LocalDateTime.now();
        String url="http://192.168.34.96:62014/jw-api/api/rtc/getNearRTCInfo";
        url=url+"?opCode=/signature="+signature().get("signature")+"/timeStamp="+signature().get("time");
        HttpUtils.Response response = HttpUtils.doGet(url, "UTF-8", null);
        String responseData = response.getResponseData();
        JSONObject jsonObject= JSONObject.parseObject(responseData);
        Object datasObject = jsonObject.get("data");
        List<Map<String , Object>> datas=new ArrayList<>();
        if(null!=datasObject){
            JSONArray jsonArray = JSONArray.parseArray(datasObject.toString());
            for (Object data : jsonArray) {
                Map map=new HashMap();
                JSONObject dataObject=JSONObject.parseObject(data.toString());
                Object state = dataObject.get("state");
                Object berthTotal = dataObject.get("berthTotal");
                Object sectionName = dataObject.get("sectionName");
                Object cantonId = dataObject.get("cantonId");
                Object areaId = dataObject.get("areaId");
                Object berthVacant = dataObject.get("berthVacant");
                Object areaName = dataObject.get("areaName");
                Object sectionLongitude = dataObject.get("sectionLongitude");
                Object id = dataObject.get("id");
                Object cantonName = dataObject.get("cantonName");
                Object sectionId = dataObject.get("sectionId");
                Object sectionLatitude = dataObject.get("sectionLatitude");
                map.put("state", state);
                map.put("berthTotal", berthTotal);
                map.put("sectionName", sectionName);
                map.put("cantonId", cantonId);
                map.put("areaId", areaId);
                map.put("berthVacant", berthVacant);
                map.put("areaName", areaName);
                map.put("sectionLongitude", sectionLongitude);
                map.put("id", id);
                map.put("cantonName", cantonName);
                map.put("sectionId", sectionId);
                map.put("sectionLatitude", sectionLatitude);
                map.put("cjsj",LocalDateTime.now());
                datas.add(map);
            }
            //清空表，插入数据
            ytcDao.delNearRTCInfo();
            ytcDao.insertNearRTCInfo(datas);
        }
    }

    public void getConstantlyBerth(){
        LocalDateTime dateTime=LocalDateTime.now();
        String url="http://192.168.34.96:62014/jw-api/api/rtc/getConstantlyBerth";
        url=url+"/opCode=/signature="+signature()+"/timeStamp="+dateTime.toEpochSecond(ZoneOffset.of("+8"));
        HttpUtils.Response response = HttpUtils.doGet(url, "UTF-8", null);
        String responseData = response.getResponseData();
        JSONObject jsonObject= JSONObject.parseObject(responseData);
        Object datasObject = jsonObject.get("data");
        List<Map<String , Object>> datas=new ArrayList<>();
        if(null!=datasObject){
            JSONArray jsonArray = JSONArray.parseArray(datasObject.toString());
            for (Object data : jsonArray) {
                Map map=new HashMap();
                JSONObject dataObject=JSONObject.parseObject(data.toString());
                Object LoadIndex = dataObject.get("LoadIndex");
                Object UseCount = dataObject.get("UseCount");
                Object AreaId = dataObject.get("AreaId");
                Object CantonId = dataObject.get("CantonId");
                Object BerthVacant = dataObject.get("BerthVacant");
                Object AreaName = dataObject.get("AreaName");
                Object NotUsedCount = dataObject.get("NotUsedCount");
                Object SectionLatitude = dataObject.get("SectionLatitude");
                Object BerthTotal= dataObject.get("BerthTotal");
                Object TotalCounts = dataObject.get("TotalCounts");
                Object CantonName = dataObject.get("CantonName");
                Object SectionName = dataObject.get("SectionName");
                Object SectionLongitude = dataObject.get("SectionLongitude");
                Object SectionId = dataObject.get("SectionId");
                map.put("LoadIndex", LoadIndex);
                map.put("UseCount", UseCount);
                map.put("AreaId", AreaId);
                map.put("CantonId", CantonId);
                map.put("BerthVacant", BerthVacant);
                map.put("AreaName", AreaName);
                map.put("NotUsedCount", NotUsedCount);
                map.put("SectionLatitude", SectionLatitude);
                map.put("BerthTotal", BerthTotal);
                map.put("TotalCounts", TotalCounts);
                map.put("SectionName", SectionName);
                map.put("CantonName", CantonName);
                map.put("SectionLongitude", SectionLongitude);
                map.put("SectionId", SectionId);
                map.put("cjsj",LocalDateTime.now());
                datas.add(map);
            }
            //清空表，插入数据
            ytcDao.delConstantlyBerth();
            ytcDao.insertConstantlyBerth(datas);
        }
    }
    public static void main1(String[] args) {
        String url="http://127.0.0.1:9099/test/data1=1qwe&&data2=2qwe";
        Map map=new HashMap();
        HttpUtils.Response response = HttpUtils.doGet(url, "UTF-8", map);
        String responseData = response.getResponseData();
        System.out.println(responseData);
    }
    void creditCard(){
        Channel channel = RabbitContUtil.getChannel();
        try {
        //声明交换器
        String exchangeName = "exchange";
        channel.exchangeDeclare(exchangeName, "direct", true);
        //声明队列
        String queueName = channel.queueDeclare().getQueue();
        String routingKey = "wendao";
        //绑定队列，通过键 hola 将队列和交换器绑定起来
        channel.queueBind(queueName, exchangeName, routingKey);
        while(true) {
            //消费消息
            boolean autoAck = false;
            String consumerTag = "";
            channel.basicConsume(queueName, autoAck, consumerTag, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String routingKey = envelope.getRoutingKey();
                    String contentType = properties.getContentType();
                    System.out.println("消费的路由键：" + routingKey);
                    System.out.println("消费的内容类型：" + contentType);
                    long deliveryTag = envelope.getDeliveryTag();
                    //确认消息
                    channel.basicAck(deliveryTag, false);
                    System.out.println("消费的消息体内容：");
                    String bodyStr = new String(body, "UTF-8");
                    System.out.println(bodyStr);
                    CreditCard creditCard=new CreditCard();
                    //推送kafka
                    KafkaConsumer<String, String> consumer = baseKafkaConsumer.getConsumer("");
                    try {
                        baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(""), "");
                        consumer.commitSync();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }  }catch (Exception e){
            e.printStackTrace();
        }
    }


}
